<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>辞职申请</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8">
    <link rel="stylesheet" href="/Sys/css/layui.css">
    <link rel="stylesheet" href="/Sys/css/sign.css">
    <link rel="stylesheet" href="/Sys/js/css/modules/layui-icon-extend/iconfont.css">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="/Sys/css/font.css">
    <link rel="stylesheet" href="/Sys/css/xadmin.css">
    <script type="text/javascript" src="/Sys/js/jquery-3.3.1.min.js"></script>
    <script src="/Sys/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
</head>

<body>
<div class="x-body">
    <form class="layui-form">
        <input type="hidden" name="userId" value="[[${session.id}]]">
        <input type="hidden" name="teacherId" value="">
        
        <div class="layui-form-item">
            <label for="jobPositionId" class="layui-form-label">
                <span class="x-red">*</span>申请岗位
            </label>
            <div class="layui-input-inline">
                <select id="jobPositionId" name="jobPositionId" required lay-verify="required" lay-filter="jobPosition">
                    <option value="">请选择岗位</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label for="remark" class="layui-form-label">
                <span class="x-red">*</span>辞职原因
            </label>
            <div class="layui-input-block">
                <textarea id="remark" name="remark" placeholder="请输入辞职原因" class="layui-textarea" required lay-verify="required"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <button class="layui-btn" lay-filter="add" lay-submit="">
                提交申请
            </button>
        </div>
    </form>
</div>
<script>
    layui.use(['form', 'layer'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer;

        // 页面加载时获取岗位列表
        var userId = '[[${session.id}]]';
        $.ajax({
            url: '/Sys/job/getUserJobPositions',
            type: 'GET',
            data: { userId: userId },
            success: function(res) {
                if(res.code === 0) {
                    var positions = res.data;
                    var select = $('select[name="jobPositionId"]');
                    positions.forEach(function(position) {
                        select.append(new Option(position.departmentName + ' - ' + position.jobType, position.id));
                    });
                    form.render('select');
                } else {
                    layer.msg('获取岗位列表失败：' + (res.msg || '未知错误'), {icon: 2});
                }
            }
        });

        // 监听岗位选择
        form.on('select(jobPosition)', function(data) {
            if(data.value) {
                // 获取对应的teacherId
                $.ajax({
                    url: '/Sys/job/getTeacherId',
                    type: 'GET',
                    data: { jobPositionId: data.value },
                    success: function(res) {
                        if(res.code === 0) {
                            $('input[name="teacherId"]').val(res.teacherId);
                        } else {
                            layer.msg('获取教师信息失败：' + (res.msg || '未知错误'), {icon: 2});
                        }
                    }
                });
            }
        });

        //监听提交
        form.on('submit(add)', function (data) {
            data.field.status = 0; // 设置初始状态为待审批
            data.field.userId =  '[[${session.id}]]';

            $.ajax({
                url: '/Sys/addResign',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(data.field),
                success: function(res) {
                    if (data != "failure") {
                        layer.msg('提交成功', {
                            icon: 1,
                            time: 2000
                        }, function() {
                            // 关闭当前frame
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            // 刷新父页面
                            parent.location.reload();
                        });
                    } else {
                        layer.msg('提交失败：' + (res.msg || '未知错误'), {icon: 2});
                    }
                }
            });
            return false;
        });
    });
</script>
</body>
</html> 